package cn.xfdzx.android.apps.shop.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.OneBeen;
import cn.xfdzx.android.apps.shop.contract.RetailLevelContract;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateMyOrderMessage;
import cn.xfdzx.android.apps.shop.fragment.RetailLevelFragment;
import cn.xfdzx.android.apps.shop.presenter.RetailLevelPresenter;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

public class RetailLevelActivity extends BaseMvpActivity<RetailLevelPresenter> implements RetailLevelContract.View {
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tl_home_type)
    TabLayout tlHomeType;
    @BindView(R.id.tv_home_type_title)
    TextView tvHomeTypeTitle;
    @BindView(R.id.iv_home_type)
    ImageView iv_home_type;
    @BindView(R.id.vp_fragment)
    ViewPager vpFragment;
    private HomeVPListAdapter homeVPAdapter;
    private List<Fragment> mFragments;
    private List<String> titleList = new ArrayList<>();
    private RelativeLayout.LayoutParams item_bar;
    private String type_id;
    private OneBeen bean;
    private int index;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_type;
    }

    @Override
    public void initView() {

        initHigh();
        type_id = getIntent().getStringExtra("type_id");
        mPresenter = new RetailLevelPresenter();
        mPresenter.attachView(this);
        tvHomeTypeTitle.setText(getIntent().getStringExtra("title_name"));
        index = getIntent().getIntExtra("index", 0);
        mFragments = new ArrayList<>();

        mPresenter.oneType(type_id, "1");

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void oneTypeSuccess(OneBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            this.bean = bean;
            titleList.clear();
            for (OneBeen.DataBean dataBean : bean.getData()) {
                titleList.add(dataBean.getName());
                mFragments.add(RetailLevelFragment.newInstance(dataBean.getId()));
                tlHomeType.addTab(tlHomeType.newTab().setText(dataBean.getName()));
            }

            homeVPAdapter = new HomeVPListAdapter(getSupportFragmentManager(), titleList, mFragments);
            vpFragment.setAdapter(homeVPAdapter);

            tlHomeType.setupWithViewPager(vpFragment);
            tlHomeType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    View view = tab.getCustomView();
                    if (null == view) {
                        tab.setCustomView(R.layout.item_textview);
                    }
                    TextView textView = tab.getCustomView().findViewById(R.id.tv_item_text);
                    textView.setTextSize(14);
                    textView.setText(tab.getText());
                    textView.setTextColor(tlHomeType.getTabTextColors());
                    textView.setTypeface(Typeface.DEFAULT_BOLD);

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    View view = tab.getCustomView();
                    if (null == view) {
                        tab.setCustomView(R.layout.item_textview);
                    }
                    TextView textView = tab.getCustomView().findViewById(R.id.tv_item_text);
                    textView.setTextSize(13);
                    textView.setText(tab.getText());
                    textView.setTextColor(tlHomeType.getTabTextColors());
                    textView.setTypeface(Typeface.DEFAULT);

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
            vpFragment.setCurrentItem(index);
            vpFragment.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                    EventBus.getDefault().post(new HomeTypeDataMessage(bean.getData().get(position).getId()));
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

            initOnPreDrawListener(bean);
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    private void initOnPreDrawListener(OneBeen oneBeen) {
        final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {

               EventBus.getDefault().post(new HomeTypeDataMessage(oneBeen.getData().get(vpFragment.getCurrentItem()).getId()));

                // 移除OnPreDrawListener事件监听
                getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });
    }
    @OnClick({R.id.iv_home_type, R.id.iv_home_type_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_home_type:
                if (Utils.isFastClick()){
                if (null != bean) {
                    Intent intent = new Intent(this, TypeActivity.class);
                    intent.putExtra("title_name", getIntent().getStringExtra("title_name"));
                    intent.putExtra("data", bean);
                    startActivityForResult(intent, START_CODE);
                }}
                break;
            case R.id.iv_home_type_back:
                finish();
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                vpFragment.setCurrentItem(data.getIntExtra("index", 0));
            }
        }
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
